package com.fh.controller.app.appuser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.fh.service.system.redis.RedisService;
import com.fh.util.*;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.system.Menu;
import com.fh.entity.system.Role;
import com.fh.entity.system.User;
import com.fh.service.system.appuser.LoginService;
import com.fh.service.system.menu.MenuService;
import com.fh.service.system.role.RoleService;
import com.fh.service.system.user.UserService;

/*
 * 登录接口
 */
@Controller
@RequestMapping(value="/appuser")
public class LoginAppController extends BaseController {

	@Resource(name="loginService")
	private LoginService loginService;
	
	@SuppressWarnings("finally")
	@RequestMapping("/login")
	@ResponseBody
    public String login(HttpSession session,HttpServletRequest request) {
		
		JSONObject js = new JSONObject();
		try {
			 PageData pd = new PageData();
			 Object parameter = pd.getParameters(request);
			 
	         JSONObject jsonObject = JSONObject.fromObject(parameter);
	         String mobile_email = jsonObject.getString("mobile_email");
	         String password = jsonObject.getString("password");
	         String type = jsonObject.getString("type");
	         pd.put("mobile_email", mobile_email);
	         pd.put("password", password);
	         pd.put("type",type);
/*	         Sys_app_user sUser = new Sys_app_user();
	         sUser.setNAME(name);
	         sUser.setPASSWORD(password);
	         sUser.setTYPE(type);*/
	         PageData list = loginService.login(pd);
	         if(null != list){
					 js.put("user_name", list.get("USERNAME"));

					 session.setAttribute("user", mobile_email);
	        	 js.put("result", "0");  //登录成功
	         }else {
	        	 js.put("result", "1");  //登录失败
			}
	         System.out.println(mobile_email+ "" +password);
		} catch (Exception e) {
			js.put("result", "2");  //服务器内部错误
			e.printStackTrace();
		}finally{
	    	 return js.toString();
		}
    }
}
